package com.youlai.quarkus.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.quarkus.system.common.result.PageResult;
import com.youlai.quarkus.system.common.result.Result;
import com.youlai.quarkus.system.model.entity.SysUser;
import com.youlai.quarkus.system.model.form.UserForm;
import com.youlai.quarkus.system.model.query.UserPageQuery;
import com.youlai.quarkus.system.model.vo.UserInfoVO;
import com.youlai.quarkus.system.model.vo.UserPageVO;
import com.youlai.quarkus.system.service.SysUserService;
import jakarta.validation.Valid;
import jakarta.ws.rs.*;
import jakarta.ws.rs.container.ContainerResponseContext;
import lombok.RequiredArgsConstructor;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.tags.Tag;

import java.io.IOException;

/**
 * 用户控制器
 *
 * @author haoxr
 * @since 2022/10/16
 */
@Tag(name = "02.用户接口")
@Path("/api/v1/users")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService userService;

    @Operation(summary = "用户分页列表")
    @GET
    @Path("page")
    public PageResult<UserPageVO> getUserPage(
            @QueryParam(value = "pageNum") Integer pageNum,
            @QueryParam(value = "pageSize") Integer pageSize,
            @QueryParam(value = "keyWord") String keyWord,
            @QueryParam(value = "status") Integer status,
            @QueryParam(value = "deptId") Long deptId
    ) {
        UserPageQuery query = new UserPageQuery(keyWord, status, deptId);
        query.setPage(pageNum, pageSize);
        IPage<UserPageVO> result = userService.getUserPage(query);
        return PageResult.success(result);
    }

    /**
     *  {@link @PermissionsAllowed} 实现自定义权限认证
     *
     * 第一层 : name
     * 第二层 : actions
     * 不支持第三层（sys:user:add）是无法识别的
     *
     * @param userId
     * @return
     */
    @Operation(summary = "用户详情")
    @GET
    @Path("getUser/{userId}")
    public Result<SysUser> getUser(@PathParam("userId") Integer userId) {
        return Result.success(userService.getUser(userId));
    }

    @Operation(summary = "新增用户")
    @POST
    public Result<Boolean> saveUser(@RequestBody @Valid UserForm userForm) {
        boolean result = userService.saveUser(userForm);
        return Result.judge(result);
    }

    @Operation(summary = "用户表单数据")
    @Path("/{userId}/form")
    @GET
    public Result<UserForm> getUserForm(
            @Parameter(description = "用户ID") @PathParam("userId") Long userId
    ) {
        UserForm formData = userService.getUserFormData(userId);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户")
    @Path(value = "/{userId}")
    @PUT
    public Result updateUser(
            @Parameter(description = "用户ID") @PathParam("userId") Long userId,
            @RequestBody @Valid UserForm userForm) {
        boolean result = userService.updateUser(userId, userForm);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户")
    @Path("/{ids}")
    @DELETE
    public Result deleteUsers(
            @Parameter(description = "用户ID，多个以英文逗号(,)分割") @PathParam("ids") String ids
    ) {
        boolean result = userService.deleteUsers(ids);
        return Result.judge(result);
    }

    @Operation(summary = "修改用户密码")
    @Path(value = "/{userId}/password")
    @PATCH
    public Result<Boolean> updatePassword(
            @Parameter(description = "用户ID") @PathParam("userId") Long userId,
            String password
    ) {
        boolean result = userService.updatePassword(userId, password);
        return Result.judge(result);
    }

    @Operation(summary = "修改用户状态")
    @Path(value = "/{userId}/status")
    @PATCH
    public Result<Boolean> updateUserStatus(
            @Parameter(description = "用户ID") @PathParam("userId") Long userId,
            @Parameter(description = "用户状态(1:启用;0:禁用)") Integer status
    ) {
        boolean flag = userService.updateUserStatus(userId, status);
        return Result.success(flag);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GET
    @Path("/me")
    public Result<UserInfoVO> getCurrentUserInfo() {
        UserInfoVO userInfoVO = userService.getCurrentUserInfo();
        return Result.success(userInfoVO);
    }

    @Operation(summary = "用户导入模板下载")
    @Path("/template")
    @GET
    public void downloadTemplate(ContainerResponseContext response) throws IOException {
    }

    @Operation(summary = "导入用户")
    @Path("/_import")
    @POST
    public Result<String> importUsers(@Parameter(description = "部门ID") @FormParam("deptId") Long deptId, @FormParam("file") String file) throws IOException {
        return null;
    }

    @Operation(summary = "导出用户")
    @Path("/_export")
    @GET
    public void exportUsers(@QueryParam(value = "pageNum") Integer pageNum,
                            @QueryParam(value = "pageSize") Integer pageSize,
                            @QueryParam(value = "keyWord") String keyWord,
                            @QueryParam(value = "status") Integer status,
                            @QueryParam(value = "deptId") Long deptId,
                            ContainerResponseContext response) throws IOException {

    }
}
